//
//  YZdetailTableView.m
//  kuaiyi
//
//  Created by wyman on 16/5/6.
//
//

#import "YZdetailTableView.h"
#import "YZShowCase.h"
#import "YZAddController.h"
#import "YZillMessage.h"
@interface YZdetailTableView()
@property (nonatomic, strong) YZShowCase *showCase;

@property (nonatomic, strong) YZillMessage *imesage;

@end
@implementation YZdetailTableView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self read];

        [self setUI];
    }
    return self;
}

-(NSMutableArray *)detailArray{
    
    
    if (_detailArray == nil) {
        _detailArray = [[NSMutableArray alloc]init];
    }
    return _detailArray;

}
-(void)setUI{

//    [self addSubview:self.showCase];
    
    self.delegate = self;
    self.dataSource = self;
    UINib *nib = [UINib nibWithNibName:@"YZShowCase" bundle:nil];
    [self registerNib:nib forCellReuseIdentifier:@"showcase"];
    self.separatorStyle = UITableViewCellSeparatorStyleNone;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didClicked:) name:@"shownotice" object:nil];

    
    
    
}

-(void)didClicked:(NSNotification *)notification{
    
    NSDictionary *weatherInfo = notification.userInfo;

    self.imesage = weatherInfo[@"ills"];
    
    [self.detailArray addObject:self.imesage];
    
    [self saveArray];
    
    [self reloadData];

}


- (void)saveArray
{
    // 1.获得沙盒根路径
    NSString *home = NSHomeDirectory();
    
    // 2.document路径
    NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];

    
    NSString *filepath = [docPath stringByAppendingPathComponent:@"case.data"];
    
    
//    NSArray *array = [NSArray arrayWithArray:self.detailArray];

    [NSKeyedArchiver archiveRootObject:self.detailArray toFile:filepath];
    

}


- (void)read {
    // 1.获得沙盒根路径
    NSString *home = NSHomeDirectory();
    
    // 2.document路径
    NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
    
    // 3.文件路径
    NSString *filepath = [docPath stringByAppendingPathComponent:@"case.data"];
    
    // 4.读取数据
    NSLog(@"%@",filepath);
    self.detailArray = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.detailArray.count?self.detailArray.count:1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *ID = @"showcase";
    YZShowCase *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.hiddenImage.hidden = YES;

    if (self.detailArray.count == 0) {
        cell.hiddenImage.hidden = NO;
   }
    
    if (self.detailArray.count !=0) {
        cell.ills = self.detailArray[indexPath.row];
        if (indexPath.row == self.detailArray.count-1) {
            cell.hiddenImage.hidden = NO;
        }
    }


    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSDictionary *dic = @{@"ills":indexPath};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"delete" object:self userInfo:dic];
 
}

@end
